//
//  HOTAddMineViewController.m
//  Genpara
//
//  Created by AndyChang.zhang on 14-9-5.
//  Copyright (c) 2014年 zhangchh. All rights reserved.
//

#import "HOTAddMineViewController.h"
#import "ZZZUIScrollView+Extends.h"
#import "UITextField+extend.h"

@interface HOTAddMineViewController () <UITextFieldDelegate, UITextViewDelegate>

@end

@implementation HOTAddMineViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_oreno"]];
    [self setNaviTitle:@"俺のシリーズ"];
    
    _sendBtn.titleLabel.font = ZZZ_fontWithName(kHOT_FONT_ROUNDED_X, 20);
    [_sendBtn.titleLabel.layer setShadowColor:[[UIColor blackColor] CGColor]];
    
    [_sendBtn.titleLabel.layer setShadowOpacity:1.f];
    [_sendBtn.titleLabel.layer setShadowOffset:CGSizeMake(5, 2)];
    _contentBgImageView.image = [_contentBgImageView.image resizableImageWithCapInsets:UIEdgeInsetsMake(26, 22, 26, 22)];
    
    _mainScrollView.delaysContentTouches = NO;
    
    UIToolbar *toolBar = [self createToolBar];
    _contentTextView.inputAccessoryView = toolBar;
    //_contentBgImageView.image = [_contentBgImageView.image stretchableImageWithLeftCapWidth:50 topCapHeight:10];
    //_contentBgImageView.frame = CGRectMake(_contentBgImageView.frame.origin.x, _contentBgImageView.frame.origin.y, _contentBgImageView.frame.size.width, 500);
}

-(UIToolbar *) createToolBar {
    
    //取消按钮
    UIToolbar * toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, ZZZ_SCREEN_WIDTH, 44)];
    [toolBar setBarStyle: UIBarStyleBlackTranslucent];
    
    //完成按钮
    UIButton *doneBtn = [[UIButton alloc] initWithFrame:CGRectMake(ZZZ_SCREEN_WIDTH - 5 -80, 10, 80, 22)];
    [doneBtn setTitle:@"Done" forState:UIControlStateNormal];
    doneBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
    doneBtn.titleLabel.font = [UIFont boldSystemFontOfSize:18];
    [doneBtn addTarget:self action:@selector(doneBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [doneBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [doneBtn setTitleColor:[UIColor colorWithWhite:0.600 alpha:1.000] forState:UIControlStateHighlighted];
    
    [toolBar addSubview:doneBtn];
    
    return toolBar;
}

-(void)doneBtnClicked:(id)sender {
    
    [_mainScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    [_contentTextView resignFirstResponder];
    
    
}

//保存
-(void)doSave {
    
    [self showLoadingWithMessage:MSG_GAMEN_LOADING];
    
    NSDictionary *parameters = @{REQ_KEY_USER_TOKEN:self.userInfo.token,
                                 REQ_KEY_USER_ID:self.userInfo.userId,
                                 REQ_KEY_CATE_NAME:_cateTitleText.text,
                                 REQ_KEY_REPLAY_CONTENT:_contentTextView.text};
    
    __unsafe_unretained HOTAddMineViewController *tempObj = self;
    [self requestWithURL:HOT_URL_ADD_SERIES_CATE Parameters:parameters success:^(NSInteger result, NSArray * data) {
        [tempObj showSuccessWithMessage:MSG_SUCCESS complection:^(void) {
            
            [tempObj.navigationController popViewControllerAnimated:YES];
        }];
    } failure:nil];
}

-(BOOL)checkParametersValid {
    
    NSString *errName = @"";
    HOTInputItemType inputType = 0;
    if ([_cateTitleText textIsNullOrEmpty]) {
        
        errName = @"标题";
        inputType = HOTInputItemText;
    } else if (_contentTextView.text == nil || [_contentTextView.text isEqualToString:@""]) {
        
        errName = @"内容";
        inputType = HOTInputItemText;
    }
    
    if (errName.length > 0) {
        
        NSString * errMsg = [self getErrMsgWith:errName itemType:inputType];
        
        ZZZPopupView *popupView = [[ZZZPopupView alloc] initWithTitle:ALERT_TITLE message:errMsg delegate:nil cancelButtonTitle:nil otherButtonTitle:ALERT_BTN_OK];
        
        [popupView showInWindow];
        
        return NO;
    }
    
    return YES;
}

#pragma mark -
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    
    return YES;
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    [_mainScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    [textField resignFirstResponder];
    
    return YES;
}

-(BOOL)textViewShouldBeginEditing:(UITextView *)textView {
    
    [_mainScrollView scrollViewToVisible:textView offsetY:40 animated:YES];
    
    return YES;
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)sendBtn:(id)sender {
    
    [_mainScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    [_contentTextView resignFirstResponder];
    
    //if ([self checkParametersValid]) {
        
        [self doSave];
    //}
}
@end
